<?php
/**
 * Page is put together
 *
 * Based on the side path the content with header, navigation
 * etc. is loaded and is issued as a page
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo 'content-type' muss in die ini Datei
 */

class Page_AppController extends Zend_Controller_Action
{

    protected $_serverPath;
    protected $_mandant = 'mario';
    /**
     * Initialize the general options
     *
     * @return void
     */
    public function init()
    {
        $this->_serverPath = realpath(APPLICATION_PATH .  '/../home/' . $this->_mandant . '/page');
    }
    /**
     * Build the page with navi, content, footer etc.
     *
     * @var string          $courseRequest   course
     * @var string          $languageRequest language
     * @var string          $pagePath        created real page path
     * @var string          $renderPath      path to render
     * @var string          $metaFile        path to meta file data
     * @var Zend_Config_Ini $metaConfig      meta config data
     * @var array           $metaDatas       meta data
     *
     * @return void
     */
    public function indexAction()
    {
var_dump($_POST);
    }
    public function templateAction()
    {
        $path = $this->_serverPath . DIRECTORY_SEPARATOR . 'template';
        $handle=opendir ($path);
        $templateList = array();
        $i = 0;
        // read template files
        while ($file = readdir ($handle)) {

            if(is_file($path.'/'.$file)) {
                
                $config = new Zend_Config_Ini($path.'/'.$file);
                $templateList[$i]['id'] = preg_replace("/\D/","",$file);
                $templateList[$i]['name'] = $config->name;
                $i++;
            }
        }
        closedir($handle);

        // Sort alphabetical list template
        $sortArray = array(); 
        foreach($templateList as $key => $array) { 
            $sortArray[$key] = $array['name']; 
        } 
        array_multisort($sortArray, SORT_ASC, SORT_STRING, $templateList); 

        foreach ($templateList as $template) {
            $this->view->folders .=
                '<li>' .
                    '<a title="template'.$template["id"].'"' .
                    'href="'.$this->view->url(array('store' => $template["id"])).'">' .
                    $template['name'] .
                    '</a>';
                '</li>';
        }
        
        $this->view->layout =
            $this->view->url(
                array('param' => 'readtemplate','store' => $this->view->store));
    }
    
    public function readtemplateAction()
    {
echo "jo";
    }
    
    public function layoutAction()
    {

        $mandant = 'mario';
        $this->view->layout =
            $this->view->baseUrl(
                'home/' . $mandant . '/page/layout/'.
                $this->view->store .
                '/layout.html'
            );
        $path = $this->_serverPath . DIRECTORY_SEPARATOR . 'layout';

        // read layout
        /*
        if (($layoutFolder = $this->getRequest()->getPost('layout'))) {

            ;
            $file = $path . '/' . $layoutFolder . '/layout.html';

            $handle = fopen ($file, "r");
            while (!feof($handle)) {
                $buffer = fgets($handle);
                $this->view->layout .= $buffer;
            }
            fclose ($handle);
        }*/
        
        // Seeking to existing layouts
        $handle=opendir ($path);

        while ($file = readdir ($handle)) {

            if($file != "." && $file != ".." && is_dir($path.'/'.$file)) {
                // create link for layout list
                $this->view->folders .=
                    '<li>' .
                    '<a href="'.$this->view->url(array('store' => $file)).'">' .
                    $file.'</a>';
                    '</li>';
            }
        }
        closedir($handle);
    }

}